package com.zn.opit.websocketdemo.service.impl;

import com.zn.opit.websocketdemo.domain.ws.WsMsgVO;
import com.zn.opit.websocketdemo.service.RecycleCustomerService;
import com.zn.opit.websocketdemo.ws.Websocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class RecycleCustomerServiceImpl implements RecycleCustomerService {

    private static final AtomicInteger count = new AtomicInteger(0);


    @Override
    public void sendMsg(WsMsgVO msgVO) {
        // 测试websocket,实现新增客户往前端推送消息，直到前端回复
        boolean flag = false;
        do {
            // 休息300毫秒
            try { Thread.sleep(300); } catch (InterruptedException e) { log.error("休息时出错~~~~~~~", e);}

            // 往前端发送消息
            flag = Websocket.sendMessage("实时数据: " + msgVO.toString(), count.get());

            if (flag) {
                log.info("停止往前端发送数据, 因为 resultFlag 为: {}, 说明前端接收到消息", flag);
            } else {
                log.info("往前端发送数据, 因为 resultFlag 为: {}", flag);
            }
        } while (!flag);
        log.info("停止往前端发送数据, 因为 delFlag 为: {}", flag);
        int count = RecycleCustomerServiceImpl.count.incrementAndGet();
        log.info("当前count: {}", count);
    }
}
